package com.tgy.clickhouse.exception;

import com.tgy.clickhouse.exception.error.BasicErrorType;
import lombok.Data;

import java.io.Serializable;

/**
 * 操作返回对象
 * @author tgy
 * @date 2021/9/2
 */
@Data
public class OperationResult<T> implements Serializable {

    private static final long serialVersionUID = 4573613428545769800L;

    public static final String STATUS_SUCCESS = "success";
    public static final String STATUS_FAILURE = "fail";
    public static final String STATUS_SUCCESS_MSG = "Your operation has succeeded!";

    public OperationResult(){
        this.status = STATUS_SUCCESS;
    }

    public OperationResult(String status, String message, T data){
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public OperationResult(String status, String message, T data, long total){
        this.status = status;
        this.message = message;
        this.data = data;
        this.total = total;
    }

    public OperationResult(String status, String message, T data, String errorCode){
        this.status = status;
        this.message = message;
        this.data = data;
        this.errorCode = errorCode;
    }

    public OperationResult(BasicErrorType errorType){
        this.status = STATUS_FAILURE;
        this.message = errorType.getMsg();
        this.errorCode = errorType.getCode();
    }


    /**
     * 操作结果状态
     */
    private String status;

    /**
     * 错误代码，各模块自行定义
     * 操作为STATUS_SUCCESS时，为空。
     */
    private String errorCode;

    /**
     * 错误简短信息
     * 操作为STATUS_SUCCESS时，为空。
     */
    private String message;

    /**
     * 操作返回的可序列化对象
     */
    private T data;

    /**
     * 数据总量，用于分页查询时使用
     */
    private long total;

    /**
     * 错误
     * @param msg
     * @return
     */
    public OperationResult<T> failure(String msg){
        this.setStatus(OperationResult.STATUS_FAILURE);
        this.setMessage(msg);
        return this;
    }

    /**
     * 成功
     * @param msg
     * @return
     */
    public OperationResult<T> success(String msg){
        this.setStatus(OperationResult.STATUS_SUCCESS);
        this.setMessage(msg);
        return this;
    }

    /**
     * 判断是否成功
     * @return
     */
    public boolean isSuccess(){
        return STATUS_SUCCESS.equals(this.getStatus());
    }


}
